package cs348a.color;

import java.awt.Color;

/**
 * 
 * 
 * @author H.S Kim
 */
public class PaletteModel {
	private Palette palette = TerrainPalette.INSTANCE; 
	
	public int contrast = 800;
	public int level = -10;
	
	public float min = 0;
	public float max = 255;
	
	/**
	 * Sets the palette.
	 * 
	 * @param p
	 */
	public void palette(Palette p) {
		palette = p;
	}
	
	/**
	 * Returns the palette.
	 * 
	 * @return
	 */
	public Palette palette() {
		return palette;
	}
	
	/**
	 * Returns the color for the given value.
	 * 
	 * @param x
	 * @return
	 */
	public Color getColor(float x) {
		return palette.getColor(transform(x));
	}
	
	/**
	 * Scales z range to [0, 255]
	 * 
	 * @param z
	 * @return
	 */
	public int transform(float x) {		
		float range = max - min;
		
		int retval = new Double((x-min)/range*contrast).intValue() + level; 
		
		if (retval < 0) return 0;
		else if (retval > 255) return 255;
		else return retval;
	}
}
